<?php

namespace zhtfrontend\models\datas;

use common\models\datas\Student;
use Yii;

/**
 * This is the model class for table "{{%class_student}}".
 *
 * @property integer $id
 * @property integer $class_id
 * @property integer $code
 * @property integer $student_id
 * @property integer $order_id
 * @property integer $status
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $class_manage_id
 * @property integer $order_sales_id
 */
class ClassStudent extends \zhtfrontend\components\bases\BaseModel
{
    public $types;
    public $lesson_id;
    public $start;
    public $num;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%class_student}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['class_manage_id', 'student_id'], 'required'],
            [['class_manage_id', 'student_id', 'status', 'end_time', 'order_sales_id','is_deposit','is_lock','is_frequency','deadline','types','lesson_id'], 'integer'],
            [['remark','start'], 'string'],
            [['num'], 'double'],
            ['start','required','when'=>function(){
                return $this->types == 2;
            }],
            ['start','check_start','on'=>['generatecheck']]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'class_manage_id' => Yii::t('zhtfrontend', '班级编号'),
            'student_id' => Yii::t('zhtfrontend', '学生编号'),
            'order_sales_id' => Yii::t('zhtfrontend', '订单编号'),
            'status' => Yii::t('zhtfrontend', '开班状态（0未开班 1开班状态 2关闭状态）'),
            'start_time' => Yii::t('zhtfrontend', '开始时间'),
            'end_time' => Yii::t('zhtfrontend', '结束时间'),
            'is_deposit' => Yii::t('zhtfrontend', '已交保证金'),
            'is_lock' => Yii::t('zhtfrontend', '是否锁定'),
            'remark' => Yii::t('zhtfrontend', '备注'),
            'is_frequency' => Yii::t('zhtfrontend', '是否限制课次（0 否 1 是）'),
            'deadline' => Yii::t('zhtfrontend', '不限课次有效期'),
            'types' => Yii::t('zhtfrontend', '生成签到方式'),
            'lesson_id' => Yii::t('zhtfrontend', '选择开始上课的课次'),
            'start' => Yii::t('zhtfrontend', '选择时间'),
            'num' => Yii::t('zhtfrontend', '生成签到课次数'),
        ];
    }

    public function extraFields()
    {
        return [
            'student_name' => function () {
                return isset($this->student) ? $this->student->name : '';
            },
            'price' => function () {
                return isset($this->ordersitems) ? $this->ordersitems->amount : '';
            },
            'age' => function () {
                return isset($this->student) ? (int)date('Y')-$this->student->age : '';
            },
            'customer_name' => function () {
                return isset($this->student->customer) ? $this->student->customer->name : '';
            },
            'tel' => function () {
                return isset($this->student->customer) ? $this->student->customer->mobile : '';
            },
            'card_id' => function () {
                return isset($this->student->customer) ? $this->student->customer->card_id : '';
            },
            'studentcheck' => function(){
               return count($this->studentCheck);
            },
            'lesson_num' =>function(){
                if($this->ordersitems->ordersitemsrelation){
                    if($this->ordersitems->ordersitemsrelation->is_frequency == 1){
                        return  $this->ordersitems->ordersitemsrelation->lesson;
                    }else{
                        return \Yii::t('zhtfrontend','不限课次');
                    }
                }
            }

        ];
    }

    public function getClassManage()
    {
        return $this->hasOne(ClassManage::className(), ['class_id' => 'class_manage_id']);
    }
    public function getStudent()
    {
        return $this->hasOne(Student::className(), ['id' => 'student_id']);
    }

    public function getOrdersitems()
    {
        return $this->hasOne(OrdersItems::className(), ['id' => 'order_sales_id']);
    }

    public function getStudentCheck(){
        return $this->hasMany(StudentCheck::className(),['class_student_id'=>'id']);
    }

    public function check_start($attribute){
         if($this->types == 2){
             $classdayArr = ClassManageTime::find()->select('class_day')->where(['class_id'=>$this->class_manage_id])->column();
             if($this->$attribute == '' ||  !in_array(date('N',strtotime($this->$attribute)),$classdayArr)){
                 $this->addError($attribute, Yii::t('zhtfrontend', '上课时间与星期相同'));
             }
             $maxLesson = ClassLesson::find()->where(['class_id'=>$this->class_manage_id])->orderBy('lesson DESC')->limit(1)->one();
             $maxdate = strtotime(date('Y-m-d',$maxLesson->class_date));
             if(strtotime($this->$attribute) <= $maxdate){
                 $this->addError($attribute, Yii::t('zhtfrontend', '时间要大于最大课次时间'));
             }
         }
    }
}
